//
//  ProductPropDeleteCell.m
//  LazyCat
//
//  Created by Abner on 16/9/22.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "ProductPropDeleteCell.h"
#import "AddProduct.h"

@interface ProductPropDeleteCell ()<UITextFieldDelegate>
{
    UIView *_topLine; //cell最上面的线，UI要求的，只有indexPath.row == 0时显示；
    UIView *_rightLine;
    UIView *_rightBGView;
    BOOL _isNeedSetFrame; //是否上面要留10，row == 0时。不用
    NSInteger _currentRow;
}
@end

@implementation ProductPropDeleteCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatProductPropDeleteCellUI];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self creatProductPropDeleteCellUI];
}
-(void)setFrame:(CGRect)frame
{
    if (_isNeedSetFrame) {
        frame = CGRectMake(frame.origin.x, frame.origin.y+10, frame.size.width, frame.size.height-10);
    }
    //既然重写了setframe方法，不仅要考虑_isNeedSetFrame，也要考虑相反的设置
    [super setFrame:frame];
}
-(void)setupProductPropDeleteCell:(LCProductProp *)aProp deleteBtTag:(NSInteger)btTag
{
    
    self.deleteBT.tag = btTag+1600;
    if (aProp) {
        _aProp = aProp;
        if (aProp.propertyname.length > 0) {
            self.propNameTf.text = aProp.propertyname;
        }else{
            self.propNameTf.text = @"";
        }
        if (aProp.propertyvalue) {
            self.propValueTf.text = aProp.propertyvalue;
        }else{
            self.propValueTf.text = @"";
        }
        
    }else{
        _aProp = [[LCProductProp alloc]init];
        self.propNameTf.text = @"";
        self.propValueTf.text = @"";
    }
    
    if (btTag == 0) {
        _isNeedSetFrame = NO;
    }else{
        _isNeedSetFrame = YES;
    }
    
    _currentRow = btTag;
    
}
-(void)creatLeftLBsUI
{
    self.propNameLb = [[UILabel alloc]init];
    [self.contentView addSubview:self.propNameLb];
    self.propNameLb.font = KAddLeftLbFont;
    self.propNameLb.textColor = KAddLeftLbColor;
    self.propNameLb.frame = CGRectMake(10, 0, 80, 44);
    self.propNameLb.text = @"属性名称";
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 44, ScreenWidth-51, 1)];
    line1.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    line1.alpha = 0.5;
    [self.contentView addSubview:line1];
    
    self.propValueLb = [[UILabel alloc]init];
    [self.contentView addSubview:self.propValueLb];
    self.propValueLb.font = KAddLeftLbFont;
    self.propValueLb.textColor = KAddLeftLbColor;
    self.propValueLb.frame = CGRectMake(10, 45, 80, 44);
    self.propValueLb.text = @"属性值";
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 89, ScreenWidth-51, 1)];
    line2.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    line2.alpha = 0.5;
    [self.contentView addSubview:line2];

}
-(void)creatRightTFsUI
{
    self.propNameTf = [[UITextField alloc]init];
    [self.contentView addSubview:self.propNameTf];
    self.propNameTf.textColor = KAddLeftLbColor;
    self.propNameTf.font = KAddLeftLbFont;
    self.propNameTf.frame = CGRectMake(90, 0, ScreenWidth-141, 44);
    self.propNameTf.placeholder = @"请输入属性名称";
    [self.propNameTf addTarget:self action:@selector(detailTfDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.propNameTf limitTextLength:30];
    
    self.propValueTf = [[UITextField alloc]init];
    [self.contentView addSubview:self.propValueTf];
    self.propValueTf.textColor = KAddLeftLbColor;
    self.propValueTf.font = KAddLeftLbFont;
    self.propValueTf.frame = CGRectMake(90, 45, ScreenWidth-141, 44);
    self.propValueTf.placeholder = @"多个用 , 隔开";
    [self.propValueTf addTarget:self action:@selector(detailTfDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
    self.propNameTf.tag = 1671;
    self.propValueTf.tag = 1672;
    
    self.propNameTf.delegate = self;
    self.propValueTf.delegate = self;
    
    self.propNameTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.propValueTf.clearButtonMode = UITextFieldViewModeWhileEditing;

    
}
-(void)detailTfDidChanged:(UITextField *)tf
{
    switch (tf.tag) {
        case 1671:
        {
            _aProp.propertyname = tf.text;
            [self setupBlock:tf.text];
        }
            break;
            
        case 1672:
        {
            _aProp.propertyvalue = tf.text;
            [self setupBlock:tf.text];
        }
            break;
        default:
            break;
    }
}
-(void)setupBlock:(NSString *)tfText
{
    if (tfText.length > 0) {
        if (self.propDeleteBlock) {
            self.propDeleteBlock(_aProp,_currentRow);
        }
    }
    
}
-(void)creatProductPropDeleteCellUI
{
    _topLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-51, 1)];
    _topLine.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    _topLine.alpha = 0.5;
    [self.contentView addSubview:_topLine];
    
    [self creatLeftLBsUI];
    [self creatRightTFsUI];
    
    _rightLine = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth-51,0 , 1, 90)];
    _rightLine.backgroundColor = SetColorRGBValue(0xe0e0e0, 1.0);
    _rightLine.alpha = 0.5;
    [self.contentView addSubview:_rightLine];
    
    _rightBGView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth-50, 0, 50, 90)];
    [self.contentView addSubview:_rightBGView];
    _rightBGView.backgroundColor = SetColorRGBValue(0xF9F9F9, 1.0);
    
    self.deleteBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightBGView addSubview:self.deleteBT];
    self.deleteBT.frame = CGRectMake(0, 20, 50, 50);
    [self.deleteBT setImage:[UIImage imageNamed:@"delect_icon"] forState:UIControlStateNormal];
    [self.deleteBT setImage:[UIImage imageNamed:@"delect_icon"] forState:UIControlStateHighlighted];
    [self.deleteBT setImageEdgeInsets:UIEdgeInsetsMake(12.5, 12.5, 12.5, 12.5)];
    [self.deleteBT addTarget:self action:@selector(deleteBtClicked) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)deleteBtClicked
{
    if ([self.delegate respondsToSelector:@selector(propDeleteBtClicked:)]) {
        [self.delegate propDeleteBtClicked:self.deleteBT];
    }
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProductPropDeleteCellIdentifier";
    ProductPropDeleteCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProductPropDeleteCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
